package com.example.api.controller;

import com.example.api.aspect.SystemControllerLog;
import com.example.api.model.entity.Inventory;
import com.example.api.model.entity.InventoryRecord;
import com.example.api.model.enums.ActionTypeEnum;
import com.example.api.model.support.RespBean;
import com.example.api.model.vo.CommodityChartVo;
import com.example.api.model.vo.PurchaseInVo;
import com.example.api.service.InventoryRecordService;
import com.example.api.service.InventoryService;
import com.example.api.service.PurchaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "库存管理")
@RestController
@RequestMapping("/api/inventory")
public class InventoryController {

    @Resource
    private InventoryService inventoryService;

    @Resource
    private InventoryRecordService inventoryRecordService;

    @Resource
    private PurchaseService purchaseService;

    @ApiOperation("查询库存")
    @GetMapping("/")
    @SystemControllerLog(description = "查询库存", actionType = ActionTypeEnum.QUERY)
    public List<Inventory> findAll() {
        return inventoryService.findAll();
    }

    @ApiOperation("出入库排行统计")
    @GetMapping("/analyze")
    @SystemControllerLog(description = "出入库排行统计", actionType = ActionTypeEnum.QUERY)
    public List<CommodityChartVo> analyze(Integer type) {
        return inventoryRecordService.analyzeCommodity(type);
    }

    @ApiOperation("根据仓库id查询仓库库存情况")
    @GetMapping("/warehouse/{id}")
    @SystemControllerLog(description = "根据仓库id查询仓库库存情况", actionType = ActionTypeEnum.QUERY)
    public List<Inventory> findByWarehouse(@PathVariable Integer id) {
        return inventoryService.findByWarehouseId(id);
    }

    @ApiOperation("根据商品id查询商品所在仓库的库存")
    @GetMapping("/commodity/{id}")
    @SystemControllerLog(description = "根据商品id查询商品所在仓库的库存", actionType = ActionTypeEnum.QUERY)
    public List<Inventory> findByCommodity(@PathVariable Integer id) {
        return inventoryService.findByCommodityId(id);
    }

    @ApiOperation("根据仓库id查询仓库出库入库记录")
    @GetMapping("/record/warehouse/{id}")
    @SystemControllerLog(description = "根据仓库id查询仓库出库入库记录", actionType = ActionTypeEnum.QUERY)
    public List<InventoryRecord> findRecordByWarehouse(@PathVariable Integer id) {
        return inventoryRecordService.findAllByWarehouseId(id);
    }

    @ApiOperation("根据商品id查询商品所在仓库出库入库情况")
    @GetMapping("/record/commodity/{id}")
    @SystemControllerLog(description = "根据商品id查询商品所在仓库出库入库情况", actionType = ActionTypeEnum.QUERY)
    public List<InventoryRecord> findRecordByCommodity(@PathVariable Integer id) {
        return inventoryRecordService.findAllByCommodityId(id);
    }

    @ApiOperation("入库")
    @PostMapping("/in")
    @SystemControllerLog(description = "入库", actionType = ActionTypeEnum.UPDATE)
    public RespBean in(@RequestBody InventoryRecord record) {
        return inventoryRecordService.in(record);
    }

    @ApiOperation("出库")
    @PostMapping("/out")
    @SystemControllerLog(description = "出库", actionType = ActionTypeEnum.UPDATE)
    public RespBean out(@RequestBody InventoryRecord record) {
        return inventoryRecordService.out(record);
    }

    @ApiOperation("查询可入库商品")
    @GetMapping("/purchase")
    @SystemControllerLog(description = "查询可入库商品", actionType = ActionTypeEnum.QUERY)
    public List<PurchaseInVo> findPurchase() {
        return purchaseService.findAll();
    }

}